package com.mjk.manager.usercenter.beelog;


import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.base.work.BaseWork;
import com.mjk.common.base.work.annotation.BeeWork;
import com.mjk.common.core.jdbc.bean.BeePage;
import com.mjk.common.core.jdbc.service.idgenerator.IdGenerateType;
import com.mjk.common.core.jdbc.service.idgenerator.IdGenerator;
import com.mjk.common.core.jdbc.service.log.model.BeeLogModel;
import com.mjk.common.core.jdbc.service.plus.idbservice.BeeDBService;
import com.mjk.common.core.jdbc.service.plus.wrap.BeeQueryWrapper;
import com.mjk.common.util.ObjectUtils;
import com.mjk.manager.usercenter.beelog.dto.BeelogDto;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@BeeWork(work = "beelog")
public class BeelogWorkProxy extends BaseWork {
	@Autowired
	private BeeDBService beeDBService;

	/**
	 * 获取beelog列表信息
     */
	public BeeResponse getBeelogList(BeelogDto dto){
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new BeeLogModel());
//		if(ObjectUtils.isNotEmpty(dto.getId())) {
//			wrapper.eq(BeeLogModel::getId,dto.getId());
//		}
		if(ObjectUtils.isNotEmpty(dto.getTranceId())) {
			wrapper.eq(BeeLogModel::getTranceId,dto.getTranceId());
		}
//		if(ObjectUtils.isNotEmpty(dto.getWork())) {
//			wrapper.eq(BeeLogModel::getWork,dto.getWork());
//		}
		if (ObjectUtils.isNotEmpty(dto.getMethodName())) {
			wrapper.like(BeeLogModel::getMethodName, dto.getMethodName());
		}
		if(ObjectUtils.isNotEmpty(dto.getItemType())) {
			wrapper.eq(BeeLogModel::getItemType,dto.getItemType());
		}
		if(ObjectUtils.isNotEmpty(dto.getCreateTime())) {
			wrapper.eq(BeeLogModel::getCreateTime,dto.getCreateTime());
		}
		wrapper.condition(" methodName is not null ");
		wrapper.orderBy(" createtime desc ");
		Map<String, Object> result = new HashMap<>();
		if(isPage(dto)) {
			wrapper.setPage(dto.getPageStart(),dto.getPageSize());
			BeePage page = beeDBService.findManyByWrapperWithPage(wrapper);
			result.put("items", page.getList());
			result.put("total", page.getPageTotal());
			return BeeError.BEE_SUC.tranceError(result);
		}else {
			List<BeeLogModel> logs = beeDBService.findManyByWrapper(wrapper);
			result.put("items", logs);
			result.put("total", logs.size());
			return BeeError.BEE_SUC.tranceError(result);
		}
	}

	public BeeResponse getEcharsLog(BeelogDto dto){
		BeeQueryWrapper wrapper = new BeeQueryWrapper(new BeeLogModel());
		wrapper.select(" DATE(createtime) AS time, COUNT(*) AS count ");
		wrapper.condition(" createtime >= CURDATE() - INTERVAL 15 DAY ");
		wrapper.groupBy(" DATE(createtime) ");
		wrapper.orderBy(" time desc ");

		List<BeeLogModel> logs = beeDBService.findManyByWrapper(wrapper);
		List<BeeLogModel> initializedLogs = new ArrayList<>();
		LocalDate today = LocalDate.now();
		Map<String, String> logMap = new HashMap<>();

		// 将现有日志数据放入映射
		for (BeeLogModel log : logs) {
			logMap.put(log.getTime(), log.getCount());
		}
		// 初始化最近10天的数据
		for (int i = 0; i < 15; i++) {
			LocalDate date = today.minusDays(i);
			String dateStr = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
			String count = logMap.getOrDefault(dateStr, "0"); // 如果没有则为0
			BeeLogModel model = new BeeLogModel();
			model.setTime(dateStr);model.setCount(count);
			initializedLogs.add(model);
		}
		return BeeError.BEE_SUC.tranceError(initializedLogs);
	}
	/**
	 * 保存或修改beelog
     */
	public  BeeResponse saveOrUpdateBeelog(BeeLogModel model){
		BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
		if(ObjectUtils.isNotEmpty(model.getId())) {
			wrapper.eq(BeeLogModel::getId,model.getId());
			beeDBService.update(wrapper);
		}else {
			model.setId(IdGenerateType.AUTO_SNOWFLAKE.getGenerator().getId(model));
			beeDBService.save(model);
		}
		return BeeError.BEE_SUC.tranceError();
	}
	/**
	 * 删除beelog
     */
	public  BeeResponse delBeelog(BeeLogModel model){
		needParams(model, BeeLogModel::getId);
		BeeQueryWrapper wrapper = new BeeQueryWrapper(model);
		wrapper.eq(BeeLogModel::getId,model.getId());
		beeDBService.delete(wrapper);
		return BeeError.BEE_SUC.tranceError();
	}
}